package com.zztl.infiniteBand.ui.view.recycleview

import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.support.v7.widget.RecyclerView
import android.text.TextPaint
import android.view.View

import com.zztl.infiniteBand.R
import com.zztl.infiniteBand.util.UiUtils


/**
 * @desc
 * @auth ${user}
 * @time 2018/7/19  11:31
 */
class HeaderDecoration(var callback: GroupInfoCallback?) : RecyclerView.ItemDecoration() {
    private val mHeaderHeight: Int
    private val mDividerHeight: Int
    private val mPaint: Paint
    //用来绘制Header上的文字
    private val mTextPaint: TextPaint
    private val mFontMetrics: Paint.FontMetrics

    private val mTextSize: Float
    private val mTextOffsetX: Float


    init {

        mHeaderHeight = UiUtils.getDimens(R.dimen.dimens_dp37)
        mDividerHeight = UiUtils.getDimens(R.dimen.dp_1)
        mTextSize = UiUtils.getDimens(R.dimen.sp_15).toFloat()
        mTextOffsetX = UiUtils.getDimens(R.dimen.dp_16).toFloat()

        mPaint = Paint()
        mPaint.color = UiUtils.getColor(R.color.color_F7F6F8)
        mPaint.isAntiAlias = true

        mTextPaint = TextPaint()
        mTextPaint.color = UiUtils.getColor(R.color.color_020d18)
        mTextPaint.textSize = mTextSize
        mTextPaint.isAntiAlias = true
        mFontMetrics = mTextPaint.fontMetrics


    }


    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        super.getItemOffsets(outRect, view, parent, state)
        //当前item在recycleview的位置
        val position = parent.getChildAdapterPosition(view)
        if (null != callback) {
            val groupInfo = callback!!.getGroupInfo(position)
            if (groupInfo != null && groupInfo.isFirstViewInGroup) {
                outRect.top = mHeaderHeight
            } else {
                outRect.top = mDividerHeight
            }
        }
    }


    override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDrawOver(c, parent, state)
        val childCount = parent.childCount
        for (i in 0 until childCount) {
            val child = parent.getChildAt(i)
            val position = parent.getChildAdapterPosition(child)
            if (null != callback) {
                val groupInfo = callback!!.getGroupInfo(position)
                val left = parent.paddingLeft
                val right = parent.width - parent.paddingRight
                if (i != 0) {
                    if (groupInfo!!.isFirstViewInGroup) {
                        val top = child.top - mHeaderHeight
                        val bottom = child.top
                        drawHeaderRect(c, groupInfo, left, top, right, bottom)
                    }
                } else {
                    var top = parent.paddingTop
                    if (groupInfo!!.isLastViewInGroup) {
                        //当itemview与headerview底部平齐的时候，判断header的顶部是否小于parent内容开始的位置，如果小于则对
                        //header.top位置更新，否则继续吸附在parent顶部
                        val suggestTop = child.bottom - mHeaderHeight
                        if (suggestTop < top) {
                            top = suggestTop
                        }
                    }

                    val bottom = top + mHeaderHeight
                    drawHeaderRect(c, groupInfo, left, top, right, bottom)

                }
            }
        }
    }

    private fun drawHeaderRect(c: Canvas, groupInfo: GroupInfo, left: Int, top: Int, right: Int, bottom: Int) {
        c.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), mPaint)

        val titleX = left + mTextOffsetX
        val titleY = bottom.toFloat() - mFontMetrics.descent - (mHeaderHeight / 4).toFloat()
        //绘制Title
        c.drawText(groupInfo.title!!, titleX, titleY, mTextPaint)
    }


    interface GroupInfoCallback {
        fun getGroupInfo(postition: Int): GroupInfo?
    }


}
